const Router=require('koa-router');
const user=new Router(); 
const sqldata=require('../sqldata')
//搜索用户信息
user.get('/search',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from user where uname like '%${val.inp}%'`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata
})

//用户信息
user.get('/',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from user`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata
})
//注销用户信息
user.delete('/del',async(ctx)=>{
    let val=JSON.parse(ctx.request.body)
    console.log(val.uid)
    let sql;
    let mydata1 = await new Promise((resolve, reject)=>{
        sql=`delete from user where uid='${val.uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    let mydata2 = await new Promise((resolve, reject)=>{
        sql=`delete from collect where uid='${val.uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=JSON.stringify('删除成功');
})
module.exports=user;